const db = require('../../mysql/index')
const bcrypt = require('bcryptjs') //加密密码
const jwt = require('jsonwebtoken');
const setting = require('../../setting')
//注册界面函数
//定义正则表达式
account_rules = /^[a-zA-Z\d]\w{3,19}[a-zA-Z\d]$/
password_rules = reg = /^[\w]{5,20}$/
exports.addlogin = (req, res) => {
    if (!bcrypt.compareSync(req.body.code, req.body.code_id)) return res.send({
        status: 1,
        message: '验证码不正确',
    })
    db.query('SELECT * from bilibili_personal where account=?', req.body.account, (err, restult) => {
        if (err) return res.send({
            status: 1,
            message: err.message
        })
        if (restult.length > 0) return res.send({
            status: 1,
            message: '账号已经被注册了'
        })
        console.log(req.body.username);
        var reg = /[\t\r\f\n\s]*/g;

        if (req.body.username.replace(reg, '') == '') return res.send({
            status: 1,
            message: '请输入用户名'
        })
        if (req.body.username.length > 10) return res.send({
            status: 1,
            mes: '用户名不能大于十个字符'
        })
        if (req.body.repassword != req.body.password) return res.send({
            status: 1,
            message: '两次密码不一样'
        })
        if (!account_rules.test(req.body.account)) return res.send({
            status: 1,
            message: '账号不合规,请输入5至20位数字'
        })
        if (!password_rules.test(req.body.password)) return res.send({
            status: 1,
            er: !password_rules.test(req.body.password),
            message: '密码不合规,请输入5位以上数字'
        })
        req.body.password = bcrypt.hashSync(req.body.password, 10)
        db.query('insert into bilibili_personal (account,password,username,first_time,vip,profile) value (?,?,?,?,?,?)', [req.body.account, req.body.password, req.body.username, Date.now(), '正式会员', 'login-out'], (err, restult) => {
            if (err) return res.send({
                status: 1,
                message: err.message
            })
            if (restult.affectedRows === 1) return res.send({
                status: 0,
                message: '注册成功'
            })
        })
    })
}

//登录的操作逻辑
exports.login = (req, res) => {
    if (!bcrypt.compareSync(req.body.code, req.body.code_id)) return res.send({
        status: 1,
        message: '验证码不正确',
    })
    db.query('select * from bilibili_personal where account=?', req.body.account, (err, restult) => {
        if (err) return res.send({
            status: 1,
            message: err.message
        })
        if (restult.length !== 1) return res.send({
            status: 1,
            message: '没有这个账号哦'
        })
        if (bcrypt.compareSync(req.body.password, restult[0].password)) {
            db.query('select id from bilibili_personal where account=?', req.body.account, (err, restult) => {
                const token = jwt.sign({
                    id: restult[0].id,
                    username: req.body.account,
                }, setting.mySecret, { expiresIn: 3600 * 24 * 90 })
                res.send({
                    status: 0,
                    message: '登录成功',
                    token: `Bearer ${token}`
                })
            })
        } else {
            res.send({
                status: 1,
                message: '密码错误'
            })
        }
    })
}


